<?php

namespace App\Http\Models\Student;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class StudentBuy extends Model
{

    public $timestamps = false;
    protected $table = "student_buy";
//    protected $attributes = [
//        'delayed' => false,
//    ];

    protected $fillable = [
        'student_id',
        'stu_status',
        'amount',
        'teacher_id',
        'begin_time',
        'end_time',
        'member_number',
        'created_at'
    ];

//    const BEGIN_TIME = 'begin_time';
//    const END_TIME = 'end_time';
//    const CREATE_AT = 'created_at';
//
//    public function setBeginTimeAttribute($value) {
//        $this->attributes['begin_time'] = strtotime($value);
//    }
//
//    public function setEndTimeAttribute($value) {
//        $this->attributes['end_time'] = strtotime($value);
//    }
//
//    public function setCreatedAtAttribute($value) {
//        $this->attributes['created_at'] = strtotime($value);
//    }
//
//    public function getBeginTimeAttribute() {
//        if ($this->attributes['begin_time']) {
//            return date('Y-m-d H:i:s', $this->attributes['begin_time']);
//        }
//        return "";
//    }
//
//    public function getEndTimeAttribute() {
//        if ($this->attributes['end_time']) {
//            return date('Y-m-d H:i:s', $this->attributes['end_time']);
//        }
//        return "";
//    }
//
//    public function getCreatedAtAttribute() {
//        if ($this->attributes['created_at']) {
//            return date('Y-m-d H:i:s', $this->attributes['created_at']);
//        }
//        return "";
//    }

    public function getUserBelongsTo()
    {
        return $this->belongsTo('App\Http\Models\Dances\Dances', 'teacher_id', 'id');
    }

    public function getUser()
    {
        return $this->belongsTo('App\Http\Models\User\User', 'teacher_id', 'id');
    }

    //获取用户购买次数
    public static function getStudentBuy(array $where)
    {
        return self::query()->where($where)->whereRaw('member_number!=surplus_number')->orderBy('created_at', 'desc')->first();
    }

    //获取团课会员数据
    public static function getStudentBuyVip(array $where)
    {
        return self::query()->where($where)->where('begin_time', '!=', '')->where('end_time', '!=', '')->orderBy('created_at', 'desc')->first();
    }

    public static function getStudentBuyNotVip(array $where)
    {
        return self::query()->where($where)->where('begin_time', '=', '')->where('end_time', '=', '')->orderBy('created_at', 'desc')->first();
    }

    public static function getStudentBuyNotVipALL(array $where)
    {
        return self::query()->with(['getUserBelongsTo' => function ($query) {
            $query->select(['id', 'begin_time', 'end_time']);
        }])->where($where)->where('begin_time', '=', '')->where('end_time', '=', '')->orderBy('created_at', 'desc')->get()->toArray();
    }

    //获取当前用户所有购买次数
    public static function getStudentBuyAll(array $where)
    {
        return self::query()->where($where)->get();
    }

    //获取团课总次数
    public static function getCountCurriculum($where){
        $select="SUM(member_number) as member_number,".
                " SUM(surplus_number) as surplus_number";
        return self::query()->select(DB::raw($select))->where($where)->first();
    }
}
